package com.mxf.example.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
    private static final Logger logger = LoggerFactory.getLogger(JwtAuthenticationEntryPoint.class);

    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authenticationException)
            throws IOException, ServletException {
        //这里的异常处理已经统一交给了全局异常处理
        // 将filter中的异常捕获，发送到error controller，交给全局异常处理器处理（springboot全局异常处理不能直接拦截filter中的异常，需要转发处理）
        request.setAttribute("filter.error", authenticationException);
        //将异常分发到/error/exthrow控制器
        request.getRequestDispatcher("/error/exthrow").forward(request, response);
        return;
    }
}